import gradio as gr
import os, asyncio
#展示多张图片
import gradio as gr

import gradio as gr


# 定义一个函数，这里只是为了演示，实际应用中这个函数可以根据滑块的值执行某些操作
# def slider_changed(value):
#     return f"当前滑块的值是: {value}"
#
#
# # 创建 Gradio 界面
# with gr.Blocks() as demo:
#     # 创建一个滑块，设置最小值、最大值、步长和初始值
#     with gr.Row(elem_id="number"):
#         slider = gr.Slider(minimum=1, maximum=10, step=1, value=1, label="选择一个数值")
#
#     # 创建一个文本输出组件，用于显示滑块的当前值
#         output_text = gr.Textbox(label="滑块值")
#
#     # 将滑块的变化绑定到 slider_changed 函数，并将输出显示在文本框中
#         slider.change(fn=slider_changed, inputs=slider, outputs=output_text)
#
# # 启动 Gradio 应用
# demo.launch()

# 假设这是你的两个图片列表
image_list_1 = ["/home/xjl/pythonCode/play_test/data2/14.jpg", "/home/xjl/pythonCode/play_test/data2/14.jpg", "/home/xjl/pythonCode/play_test/data2/14.jpg","/home/xjl/pythonCode/play_test/data2/14.jpg", "/home/xjl/pythonCode/play_test/data2/14.jpg", "/home/xjl/pythonCode/play_test/data2/14.jpg"]
image_list_2 = ["/home/xjl/pythonCode/play_test/data2/15.jpg", "/home/xjl/pythonCode/play_test/data2/15.jpg","/home/xjl/pythonCode/play_test/data2/15.jpg","/home/xjl/pythonCode/play_test/data2/15.jpg", "/home/xjl/pythonCode/play_test/data2/15.jpg","/home/xjl/pythonCode/play_test/data2/15.jpg"]


def display_images():
    # 创建一个新的图片列表，每对图片放在同一行
    paired_images = []
    for img1, img2 in zip(image_list_1, image_list_2):
        paired_images.append(img1)
        paired_images.append(img2)

    return paired_images
signal = 0
def show():
    global signal
    signal += 1
    print("signal:",signal)
    if signal % 2 == 0:
        return display_images()
    else:
        return []


with gr.Blocks() as demo:
    with gr.Row():
        # 创建一个按钮
        button = gr.Button("显示图片")

    with gr.Row():
        # 创建一个 Gallery 组件来显示图片
        gallery = gr.Gallery(columns=2)

    # 将按钮点击事件绑定到 display_images 函数
    button.click(fn=show, outputs=gallery)

# 启动 Gradio 应用
demo.launch()


#d读取文件夹地址
import tkinter as tk
from tkinter.filedialog import askopenfilename, askdirectory, asksaveasfilename, askopenfilenames

# def tk_window_asksavefile(init_dir=os.getcwd(), suffix = '') -> str:
#     window = tk.Tk()
#     window.wm_attributes('-topmost', 1)
#     window.withdraw()
#     #filename = askopenfilename(initialdir=init_dir, filetypes=[('', suffix)])
#     filename = askdirectory(initialdir=os.getcwd())
#     return filename
#
# async def tk_asksavefile_asy(init_dir = os.getcwd(), suffix = '') -> str:
#     fname = await asyncio.to_thread(tk_window_asksavefile, init_dir, suffix)
#     return fname
#
#
# with gr.Blocks() as demo:
#     norm_input_text = gr.Text(label='Input tif for normalization')
#     norm_input_btn = gr.Button('Select input tif')
#     norm_input_btn.click(tk_asksavefile_asy, inputs=[], outputs=[norm_input_text])
#     demo.queue(max_size=2044).launch(server_name="127.0.0.1",inbrowser=True,quiet=True,
#                                                             share=False)
#
# demo.launch()

# 学习一些方法的使用
import gradio as gr

# with gr.Blocks() as demo:
#     with gr.Row():
#         gr.Dropdown(["模型1", "模型2", "模型3"], label="Stable Diffusion模型")
#         gr.Dropdown(["模型1", "模型2", "模型3"], label="外挂VAE模型")
#         gr.Slider(label="CLIP终止层数")
#
#     with gr.Tab("文生图"):
#         with gr.Row():
#             with gr.Column(scale=5):
#                 gr.Textbox(label="提示词", placeholder="请输入提示词", lines=5)
#                 gr.Textbox(label="反向提示词", placeholder="请输入反向提示词", lines=2)
#             # with gr.Column(scale=3):
#             #     gr.Button("生成", variant="primary")
#             #     with gr.Row():
#             #         gr.Button("", icon=".\\resources\\arrow.png")
#             #         gr.Button("", icon=".\\resources\\file.png")
#             #         gr.Button("", icon=".\\resources\\delete.png")
#             #     gr.Dropdown(["模型1", "模型2", "模型3"], label="Stable Diffusion模型")
#
#         with gr.Tab("生成"):
#             with gr.Row():
#                 with gr.Column():
#                     gr.Slider(label="采样步数")
#                     gr.Radio(["DPM++2M Karras", "DPM++ SDE Karras", "DPM++2M SDE Exponential",
#                               "DPM++2M SDE Karras", "Euler a", "Eular", "LMS", "Heun", "DPM2",
#                               "DPM2 a", "DPM++ 2M", "DPM++ SDE", "DPM++ 2M SDE", "DPM++ 2M SDE Heun",
#                               "DPM Fast", "DPM Adaptive", "DPM Adaptive Karras", "LMS Karras", "DPM2 Karras",
#                               "DPM2 a Karras", "DPM++ 2M Karras", "DPM++ 2M SDE Karras", "DPM++ 2M SDE Karras Heun"],
#                              label="采样方法")
#                     with gr.Accordion("高分辨率修复"):  # 折叠排版
#                         with gr.Row():
#                             gr.Dropdown(["模型1", "模型2"], label="放大算法")
#                             gr.Slider(label="高分迭代步数")
#                             gr.Slider(label="重绘幅度")
#                         with gr.Row():
#                             gr.Slider(label="放大倍数")
#                             gr.Slider(label="将宽度调整为")
#                             gr.Slider(label="将高度调整为")
#                     with gr.Accordion("Refiner"):
#                         with gr.Row():
#                             gr.Dropdown(["模型1", "模型2"], label="模型")
#                             gr.Slider(label="切换时机")
#                     with gr.Row():
#                         with gr.Column(scale=3):
#                             gr.Slider(label="宽度")
#                             gr.Slider(label="高度")
#                         with gr.Column(scale=1, min_width=1):
#                             gr.Slider(label="总批次", min_width=1)
#                             gr.Slider(label="单批数量", min_width=1)
#                     gr.Slider(label="提示词引导系数")
#                 # with gr.Column():
#                 #     gr.Gallery([
#                 #         ".\\resources\\1.jpg",
#                 #         ".\\resources\\2.jpg",
#                 #         ".\\resources\\3.jpg",
#                 #         ".\\resources\\4.jpg",
#                 #         ".\\resources\\5.jpg",
#                 #         ".\\resources\\6.jpg",
#                 #         ".\\resources\\7.jpg",
#                 #         ".\\resources\\8.jpg",
#                 #         ".\\resources\\9.jpg",
#                 #         ".\\resources\\10.jpg",
#                 #     ], columns=4)
#                 #     with gr.Row():
#                 #         gr.Button("发送到重绘")
#                 #         gr.Button("发送到后期处理")
#                 #         gr.Button("下载")
#                 #     gr.Textbox(label="图像信息", lines=5)
#             pass
#         with gr.Tab("嵌入式"):
#             pass
#         with gr.Tab("超网络"):
#             pass
#         with gr.Tab("模型"):
#             pass
#         with gr.Tab("LORA"):
#             pass
#     with gr.Tab("图生图"):
#         pass
#     with gr.Tab("后期处理"):
#         pass
#     with gr.Tab("PNG图片信息"):
#         pass
#     with gr.Tab("模型训练"):
#         pass
#     with gr.Tab("模型融合"):
#         pass
#     with gr.Tab("训练"):
#         pass
#
# demo.launch()


